
#include <ioLib.h>
#include <semLib.h>
#include <stdio.h>
#include <taskHookLib.h>
#include <taskLib.h>
#include <time.h>
#include <string.h>


#include "config.h"
#include "tLog.h"


void tLog()
{
  
	Trame * trame;
	char again;
    journalDeBord = open(FICHIER_LOG, O_CREAT | O_RDWR, FILEMODE);
    if (journalDeBord == ERROR)
    {
        perror("[JOURNAL] Impossible d'ouvrir le fichier de log.");
        return;
    }
	DBG("Ouvrir un Fichier Log");
    lseek(journalDeBord, 0, SEEK_END); /*Trouve la fin de fichier*/
	do{		
	
		trame = (Trame *) malloc(sizeof(Trame));
		
        //if((msgQReceive(balMessageLog, buffer, TAILLEMSG_BALMESSAGELOG, WAIT_FOREVER)) != ERROR)
		if((msgQReceive(balMessageLog, (char *) trame, sizeof(Trame), WAIT_FOREVER)) != ERROR)
        {
    	
    		again = trame->content[0] != TRAME_FIN;
    		if(!again) 
    		{
    			break; 
    		    msgQSend(balMsgEnv, (char *) trame, sizeof(Trame), WAIT_FOREVER, MSG_PRI_NORMAL);
    		}
        	
    		write(journalDeBord, trame->content, trame->lenght);	
    		
        }
        else{
        	DBG("Erreur de lecture de message Log");
        }
		taskDelay(6);
		free(trame);
	} while(again);

	DBG("Fermeture du fichier de log.");
	close(journalDeBord);
	
	
	//taskResume(pidMere);	
	DBG("Fin de Journal.");
} 
